package com.supplychainapi.controller;


import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.joindata.inf.common.util.basic.JsonUtil;
import com.supplychainapi.common.ApiVersion;
import com.supplychainapi.common.BizCode;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.model.ScmTbsupplier;
import com.supplychainapi.model.ScmTbsupplierExtend;
import com.supplychainapi.model.ScmTbtreebuild;
import com.supplychainapi.param.ScmTbtreebuildParam;
import com.supplychainapi.service.IScmTbsupplierService;
import com.supplychainapi.service.IScmTbtreebuildService;
import com.supplychainapi.utils.Constants;
import com.supplychainapi.utils.PathUitls;
import io.swagger.annotations.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

/**
 *
 * 手机版供应商
 * @author:xiezhiwei
 * @email:xiezhiwei@niwodai.net
 * @date:2017-10-12
 *
 */

@RequestMapping(PathUitls.ROOT_PHONE + PathUitls.ROOT_VERSION+PathUitls.ROOT_SUPPLIER)
@RestController
@Api(value = "SupplierPhoneController", description = "手机版供应商")
public class SupplierController extends BaseController {

	private Logger logger = LogManager.getLogger(SupplierController.class);


	@Autowired
	private IScmTbsupplierService scmTbsupplierService;

	//树结构
	@Autowired
	private IScmTbtreebuildService scmTbtreebuildService;

	@ApiVersion(1)
	@ApiOperation("新增供应商手机版")
	@RequestMapping(value = "/addSupplierPhoneInfo", method=RequestMethod.POST)
	public RestResponse<ScmTbsupplier>  addSupplierPhoneInfo(@RequestBody ScmTbsupplier supplier,@ApiIgnore LoginCache loginCache,HttpServletRequest request){

		logger.info("参数信息 ----- supplier={}", supplier == null ? null : JsonUtil.toJSON(supplier,false));

		if(StringUtils.isEmpty(supplier.getFsSupplierId())){
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "供应商代码不能为空");
		}

		if(StringUtils.isEmpty(supplier.getFsSupplierName())){
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "供应商名称不能为空");
		}

		if(StringUtils.isEmpty(supplier.getFsNodeCode())){

			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "供应商分类ID不能为空");
		}

		if(StringUtils.isEmpty(supplier.getFsNodeFullText())){
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "树节点内容全名不能为空");
		}
		supplier.setFsShopGUID(loginCache.getFsShopGUID());
		supplier.setFsCreateUserId(loginCache.getFsUserId());
		supplier.setFsCreateUserName(loginCache.getFsUserName());
		supplier.setFsUpdateUserId(loginCache.getFsUserId());
		supplier.setFsUpdateUserName(loginCache.getFsUserName());
		try {
			Boolean bl = scmTbsupplierService.getSupplierNameIsExit(loginCache.getFsShopGUID(), supplier.getFsSupplierName(), supplier.getFsSupplierId(), "add");
			if(bl){
				return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.SUPPLIER_ERROR_MESSAGE.getNum(), "当前供应商名称已存在");
			}
			scmTbsupplierService.addScmTbsupplierPhoneInfo(supplier);
		} catch (ServiceException e) {
			e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
        }

		return RestResponse.success();
	}

	@ApiVersion(1)
	@ApiOperation("更新供应商手机版")
	@RequestMapping(value = "/updateSupplierPhoneInfo", method=RequestMethod.POST)
	public RestResponse<ScmTbsupplier>  updateSupplierPhoneInfo(@RequestBody ScmTbsupplier supplier,@ApiIgnore LoginCache loginCache, HttpServletRequest request){

		logger.info("参数信息 ----- orderInVo={}", supplier == null ? null : JsonUtil.toJSON(supplier,false));

		if(supplier.getFsSupplierId()==null){
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "供应商代码不能为空");
		}

		if(supplier.getFsSupplierName()==null){
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "供应商名称不能为空");
		}
		supplier.setFsShopGUID(loginCache.getFsShopGUID());
		supplier.setFsUpdateUserId(loginCache.getFsUserId());
		supplier.setFsUpdateUserName(loginCache.getFsUserName());
		supplier.setFsUpdateTime(new Date());
		try {
			Boolean bl = scmTbsupplierService.getSupplierNameIsExit(loginCache.getFsShopGUID(), supplier.getFsSupplierName(), supplier.getFsSupplierId(), "update");
			if(bl){
				return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.SUPPLIER_ERROR_MESSAGE.getNum(), "当前供应商名称已存在");
			}
			scmTbsupplierService.updateScmTbsupplierPhoneInfo(supplier);
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}

		return RestResponse.success();
	}

	@ApiVersion(1)
	@ApiOperation("删除供应商手机版")
	@RequestMapping(value = "/deleteSupplierPhoneInfo", method=RequestMethod.GET)
	public RestResponse<ScmTbsupplier> deletePhoneSupplierInfo(@ApiParam(value = "供应商代码", required = true) @RequestParam(required = true) String fsSupplierId, @ApiIgnore LoginCache loginCache, HttpServletRequest request) {

		logger.info("参数信息 ----- fsSupplierId={},fsShopGUID={}", fsSupplierId,loginCache.getFsShopGUID());

		if(fsSupplierId==null || fsSupplierId.trim().equals("")){
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "供应商代码不能为空");
		}

		if(loginCache.getFsShopGUID()==null || loginCache.getFsShopGUID().trim().equals("")){
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getNum(), "门店编号不能为空");
		}
		List<String> fsSupplierIdList = new ArrayList<String>();
		fsSupplierIdList.add(fsSupplierId);
		List<String> fsShopGUIDList = new ArrayList<String>();
		fsShopGUIDList.add(loginCache.getFsShopGUID());
		try {
            scmTbsupplierService.deletePhoneSupplierInfo(fsSupplierIdList, fsShopGUIDList);
        } catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}

		return RestResponse.success();
	}

	/*
	@ApiVersion(1)
	@ApiOperation("获取供应商手机版列表信息")
	@RequestMapping(value = "/getSupplierPhoneListInfo", method=RequestMethod.GET)
	public  RestResponse<List<ScmTbsupplier>>  getSupplierPhoneListInfo(@ApiIgnore LoginCache loginCache,@ApiParam(value = "分类代码", required = true)@RequestParam String fsNodeCode,@ApiParam(value = "页码", required = true)@RequestParam Integer page,@ApiParam(value = "显示条数", required = true)@RequestParam Integer pageSize){
		logger.info("fsShopGUID={},fsNodeCode={},page={},pageSize={}",loginCache.getFsShopGUID(),fsNodeCode,page,pageSize);
		List<ScmTbsupplier> suppliersList = null;
		try {
			suppliersList = scmTbsupplierPhoneService.getSupplierPhoneListInfo(loginCache.getFsShopGUID(),fsNodeCode,page,pageSize);
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "查询供应商列表异常");
		}
		return RestResponse.success(suppliersList);
	}*/


	@ApiVersion(1)
	@ApiOperation("获取供应商手机版列表全部信息")
	@RequestMapping(value = "/getSupplierAllPhoneListInfo", method=RequestMethod.GET)
	public RestResponse<List<ScmTbsupplierExtend>> getSupplierAllPhoneListInfo(@ApiIgnore LoginCache loginCache, @ApiParam(value = "页码", required = true) @RequestParam(required = true) Integer page, @ApiParam(value = "显示条数", required = true) @RequestParam(required = true) Integer pageSize) {
		logger.info("fsShopGUID={},page={},pageSize={}",loginCache.getFsShopGUID(),page,pageSize);
		List<ScmTbsupplierExtend> suppliersList = null;
		try {
            suppliersList = scmTbsupplierService.getSupplierAllPhoneListInfo(loginCache.getFsShopGUID(), page, pageSize);
        } catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success(suppliersList);
	}

	@ApiVersion(1)
	@ApiOperation("获取供应商手机版列表父节点以下的信息")
	@RequestMapping(value = "/getSupplierParentAllPhoneListInfo", method=RequestMethod.GET)
	public RestResponse<List<ScmTbsupplierExtend>> getSupplierParentAllPhoneListInfo(@ApiIgnore LoginCache loginCache, @ApiParam(value = "分类代码", required = true) @RequestParam(required = true) String fsNodeCode, @ApiParam(value = "页码", required = true) @RequestParam(required = true) Integer page, @ApiParam(value = "显示条数", required = true) @RequestParam(required = true) Integer pageSize) {
		logger.info("fsShopGUID={},fsNodeCode={},page={},pageSize={}",loginCache.getFsShopGUID(),fsNodeCode,page,pageSize);
		List<ScmTbsupplierExtend> suppliersList = null;
		try {
            suppliersList = scmTbsupplierService.getSupplierParentAllPhoneListInfo(loginCache.getFsShopGUID(), fsNodeCode, page, pageSize);
        } catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success(suppliersList);
	}


	@ApiVersion(1)
	@ApiOperation("根据供应商名称模糊查询供应商手机版列表信息")
	@RequestMapping(value = "/getSupplierPhoneByFsSupplierNameListInfo", method=RequestMethod.GET)
	public RestResponse<List<ScmTbsupplierExtend>> getSupplierPhoneByFsSupplierNameListInfo(@ApiIgnore LoginCache loginCache, @ApiParam(value = "供应商名称", required = true) @RequestParam String fsSupplierName, @ApiParam(value = "页码", required = true) @RequestParam Integer page, @ApiParam(value = "显示条数", required = true) @RequestParam Integer pageSize) {
		logger.info("fsShopGUID={},fsSupplierName={},page={},pageSize={}",loginCache.getFsShopGUID(),fsSupplierName,page,pageSize);
		List<ScmTbsupplierExtend> suppliersByFsSupplierNameList = null;
		try {
            suppliersByFsSupplierNameList = scmTbsupplierService.getSupplierPhoneByFsSupplierNameListInfo(loginCache.getFsShopGUID(), fsSupplierName, page, pageSize);
        } catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success(suppliersByFsSupplierNameList);
	}


	@ApiVersion(1)
	@ApiOperation("根据供应商代码查询供应商详情")
	@RequestMapping(value = "/getSupplierPhoneDetailsInfo", method=RequestMethod.GET)
	public  RestResponse<ScmTbsupplier>  getSupplierPhoneDetailsInfo(@ApiIgnore LoginCache loginCache,@ApiParam(value = "供应商代码", required = true)@RequestParam String fsSupplierId){
		logger.info("fsShopGUID={},fsSupplierName={}",loginCache.getFsShopGUID(),fsSupplierId);
		ScmTbsupplierExtend supplierDetails = null;
		try {
            supplierDetails = scmTbsupplierService.getSupplierPhoneDetailsInfo(loginCache.getFsShopGUID(), fsSupplierId);
        } catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success(supplierDetails);
	}


    @ApiVersion(1)
    @ApiOperation("根据供应商列表关键字查询")
    @RequestMapping(value = "/getSupplierKeyWordListInfo", method = RequestMethod.GET)
    public RestResponse<List<ScmTbsupplierExtend>> getSupplierKeyWordListInfo(@ApiIgnore LoginCache loginCache, @ApiParam(value = "关键字(供应商名称关键字和代码)", required = false) @RequestParam(required = false) String keyWords, @ApiParam(value = "页码", required = true) @RequestParam Integer page, @ApiParam(value = "显示条数", required = true) @RequestParam Integer pageSize) {
        logger.info("fsShopGUID={},fsSupplierName={},page={},pageSize={}", loginCache.getFsShopGUID(), keyWords, page, pageSize);
        List<ScmTbsupplierExtend> suppliersByKeyWordsList = null;
        try {
            suppliersByKeyWordsList = scmTbsupplierService.getSupplierKeyWordListInfo(loginCache.getFsShopGUID(), keyWords, page, pageSize);
        } catch (ServiceException e) {
            e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
        }
        return RestResponse.success(suppliersByKeyWordsList);
    }

	@ApiVersion(1)
	@ApiOperation("获取供应商手机版分类树结构信息")
	@RequestMapping(value = "/getTreebuildPhoneAllInfo", method=RequestMethod.GET)
	public  RestResponse<List<ScmTbtreebuild>>  getTreebuildPhoneAllInfo(@ApiIgnore LoginCache loginCache){
		logger.info("fsShopGUID = {}",loginCache.getFsShopGUID());
		List<ScmTbtreebuild> resulScmTbtreebuildtList=null;
		try {
			resulScmTbtreebuildtList = scmTbtreebuildService.getScmTbtreebuildAllInfo(loginCache.getFsShopGUID(), Constants.SUPPLIER_CLASS);
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return  RestResponse.success(resulScmTbtreebuildtList);
	}

	@ApiVersion(1)
	@ApiOperation("根据分类名称查询供应商手机版分类树结构信息")
	@RequestMapping(value = "/getTreebuildByFsNodeTextAllInfo", method=RequestMethod.GET)
	public  RestResponse<List<ScmTbtreebuild>>  getTreebuildByFsNodeTextAllInfo(@ApiIgnore LoginCache loginCache,@ApiParam(value = "分类名称", required = true)@RequestParam String fsNodeText){
		logger.info("fsShopGUID = {},fsNodeText={}",loginCache.getFsShopGUID(),fsNodeText);
		List<ScmTbtreebuild> resulScmTbtreebuildtList=null;
		try {
			resulScmTbtreebuildtList = scmTbtreebuildService.getScmTbtreebuildByFsNodeTextAllInfo(loginCache.getFsShopGUID(), Constants.SUPPLIER_CLASS,fsNodeText);
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return  RestResponse.success(resulScmTbtreebuildtList);
	}


	@ApiVersion(1)
	@ApiOperation(value="根据父节点查询供应商分类子信息", notes="")
	@RequestMapping(value = "/getPhoneSupplierClass", method=RequestMethod.GET)
	public RestResponse<List<ScmTbtreebuild>> getPhoneSupplierClass( @ApiIgnore LoginCache loginCache,@ApiParam(value = "父节点", required = true)@RequestParam String fdParentNodeCode, HttpServletRequest request) throws ServiceException {
		logger.info("fdParentNodeCode = {}",fdParentNodeCode);
		return RestResponse.success(scmTbtreebuildService.getScmTbtreebuildAllInfos(loginCache.getFsShopGUID(), Constants.SUPPLIER_CLASS,fdParentNodeCode));
	}

	/**
	 @ApiOperation("获取供应商手机版一级分类信息")
	 @RequestMapping(value = "/getScmTbtreebuildOneLevelInfos", method=RequestMethod.GET)
	 public  RestResponse<List<ScmTbtreebuild>>  getScmTbtreebuildOneLevelInfos(@ApiParam(value = "门店ID", required = true)@RequestParam String fsShopGUID,@ApiParam(value = "页码", required = true)@RequestParam Integer page,@ApiParam(value = "数据大小", required = true)@RequestParam Integer pageSize){
	 logger.info("fsShopGUID = {}",fsShopGUID);
	 List<ScmTbtreebuild> resulScmTbtreebuildtList=null;
	 try {
	 resulScmTbtreebuildtList = scmTbtreebuildService.getScmTbtreebuildOneLevelInfos(fsShopGUID,page,pageSize,"Supplier");
	 } catch (ServiceException e) {
	 e.printStackTrace();
	 return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), "获取供应商一级分类信息访问异常");
	 }
	 return  RestResponse.success(resulScmTbtreebuildtList);
	 }
	 **/


	@ApiVersion(1)
	@ApiOperation("增加供应商分类手机版")
	@RequestMapping(value = "/addTreebuildPhoneInfo", method=RequestMethod.POST)
	public RestResponse<ScmTbtreebuild>  addTreebuildPhoneInfo(@RequestBody ScmTbtreebuildParam model,@ApiIgnore LoginCache loginCache, HttpServletRequest request){
		logger.info("参数信息 ----- model={}", model == null ? null : JsonUtil.toJSON(model,false));
		if(model==null){
			return RestResponse.fail(BizCode.FAIL.getNum(), "新增供应商分类参数不能为空");
		}
		model.setFsShopGUID(loginCache.getFsShopGUID());
		model.setFsTreeTypeId(Constants.SUPPLIER_CLASS);
		model.setFsCreateUserId(loginCache.getFsUserId());
		model.setFsCreateUserName(loginCache.getFsUserName());
		model.setFsUpdateUserId(loginCache.getFsUserId());
		model.setFsUpdateUserName(loginCache.getFsUserName());
		try {
			boolean bl = scmTbtreebuildService.saveTreeBuild(model);
			if(!bl){
				return RestResponse.fail(BizCode.FAIL.getNum(), "新增供应商分类失败");
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success();
	}

	@ApiVersion(1)
	@ApiOperation("更新供应商分类手机版")
	@RequestMapping(value = "/updateTreebuildPhoneInfo", method=RequestMethod.POST)
	public RestResponse<ScmTbtreebuild>  updateTreebuildPhoneInfo(@RequestBody ScmTbtreebuildParam model,@ApiIgnore LoginCache loginCache, HttpServletRequest request){
		logger.info("参数信息 ----- model={}", model == null ? null : JsonUtil.toJSON(model,false));
		if(model==null){
			return RestResponse.fail(BizCode.FAIL.getNum(), "更新供应商分类参数不能为空");
		}
		model.setFsShopGUID(loginCache.getFsShopGUID());
		try {
			boolean bl = scmTbtreebuildService.updateTreeBuild(model);
			if(!bl){
				return RestResponse.fail(BizCode.FAIL.getNum(), "更新供应商分类失败");
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success();
	}

	@ApiVersion(1)
	@ApiOperation("删除供应商分类手机版")
	@RequestMapping(value = "/deleteTreebuildPhoneInfo", method=RequestMethod.POST)
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "fsTreeTypeId", value = "分类类型;swagger", required = true, dataType = "string", defaultValue = ""),
			@ApiImplicitParam(paramType = "query", name = "fsNodeCode", value = "分类代码;swagger", required = true, dataType = "string", defaultValue = ""),
	})
	public RestResponse<ScmTbtreebuild>  deleteTreebuildPhoneInfo(@ApiIgnore LoginCache loginCache,@ApiIgnore @RequestBody ScmTbtreebuildParam model, HttpServletRequest request){

		logger.info("参数信息 ----- model={}", model == null ? null : JsonUtil.toJSON(model,false));

		if(model==null){
			return RestResponse.fail(BizCode.FAIL.getNum(), "删除供应商分类参数不能为空");
		}
		try {
			model.setFsShopGUID(loginCache.getFsShopGUID());
			boolean bl = scmTbtreebuildService.deleteTreeBuild(model);
			if(!bl){
				return RestResponse.fail(BizCode.FAIL.getNum(), "删除供应商分类失败");
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
		}
		return RestResponse.success();
	}

	@ApiVersion(1)
	@ApiOperation("查询供应商分类详情信息")
	@RequestMapping(value = "/getSupplierByPhoneFsSupplierNameInfo", method=RequestMethod.POST)
	public  RestResponse<ScmTbtreebuild>  getSupplierByPhoneFsSupplierNameInfo(@ApiIgnore LoginCache loginCache,@RequestBody ScmTbtreebuildParam model){
		logger.info("参数信息 ----- model={}", model == null ? null : JsonUtil.toJSON(model,false));
		if(model==null){
			return RestResponse.fail(BizCode.FAIL.getNum(),BizCode.PARAM_NULL.getNum(), "参数不能为空");
		}
		model.setFsShopGUID(loginCache.getFsShopGUID());
		ScmTbtreebuild scmTbtreebuild=null;
		try {
			scmTbtreebuild = scmTbtreebuildService.selectByKey(model);
		} catch (ServiceException e) {
			e.printStackTrace();
            return RestResponse.fail(BizCode.FAIL.getNum(), BizCode.DATA_BASE_ERROR_MESSAGE.getNum(), e.getMessage());
        }
		return  RestResponse.success(scmTbtreebuild);
	}

}
